#!/usr/bin/perl -w
use strict;
use Getopt::Long;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    re_find_filter.pl
                --from_regex "([^\t]+)\t"
                --to_regex "\$1"
				--keep_non_matching_lines
				[--help]

	Iterates through files and filters each line according to the supplied
		regular expression
	Equivalent to running over each line
		/from_regex/;
		print to_regex;

	Prints to STDOUT
USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory
my $to_regex;
my $from_regex;
my $keep_non_matching_lines;

# optional parameters
my $verbose;
my $histogram;
my $help;
GetOptions(
			'to_regex=s'				=> \$to_regex,
			'from_regex=s'				=> \$from_regex,
			'keep_non_matching_lines'	=> \$keep_non_matching_lines,
			'help'						=> \$help);

die $usage if ($help);
die $usage unless (defined $from_regex && defined $to_regex);

my $cnt_lines = 1;
my $cmd = <<"CMD";
	for (<>)
	{
		if (/$from_regex/)
		{
			print \"$to_regex\";
		}
		elsif (defined \$keep_non_matching_lines)
		{
			print;
		}
	}
CMD
#for (<>){ /$from_regex/; print STDERR ++$cnt_lines, \"\\n\"; print \"$to_regex\"; }";
#print STDERR $cmd, "\n";

 eval $cmd ;
